package demo_JUC.kuang.lock;

import javax.sound.midi.Soundbank;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// Synchronized
public class Demo01 {
    public static void main(String[] args) {
        Phone phone1 = new Phone();
        Phone phone2 = new Phone();

        new Thread(()->{
            phone1.sms();
        },"A").start();


        new Thread(()->{
            phone1.sms();
        },"B").start();
    }
}

class Phone{
    Lock lock = new ReentrantLock();

    public synchronized void sms(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "---sms");
            call(); // 这里也有锁
            call(); // 这里也有锁
            call(); // 这里也有锁
            call(); // 这里也有锁
        }finally {
            lock.unlock();
        }
    }

    public synchronized void call(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "---call");
        }finally {
            lock.unlock();
        }
    }
}